/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEmailInfo;
import org.kuali.rice.kim.util.KimConstants;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityEmailInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String emailAddressAttributeName = null;
  private String suppressionAttributeName = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityEmailInfo keei = new KimEntityEmailInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      // currently, this assumes that an entity only has one email (the one aligned
      // with this ldap record.  this is a decent assumption for now, but a configurable
      // option for either doing it this way or mapping multiple emails would be better
      // perhaps some organizations have multi-valued attributes that list emails

      // the entityEmailId is a synthetic identifier for the instance of an entity name.
      // this should be unique in the system, but there is no graceful way to do this
      // from an ldap mapping
      keei.setEntityEmailId("1");//is there an ldap attribute for this?
      keei.setEntityTypeCode("PERSON");//is there an ldap attribute for this?
      keei.setEmailTypeCode("WRK");//is there an ldap attribute for this?
      keei.setEmailAddressUnmasked(LdapUtilities.materialize(context.getStringAttribute(emailAddressAttributeName)));
      String suppressionAttributeValue = context.getStringAttribute(suppressionAttributeName);
      if (suppressionAttributeValue != null && suppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keei.setSuppressEmail(true);
        keei.setEmailAddress(KimConstants.RESTRICTED_DATA_MASK);
      } else {
        keei.setSuppressEmail(false);
        keei.setEmailAddress(LdapUtilities.materialize(context.getStringAttribute(emailAddressAttributeName)));
      }
      keei.setActive(true);//is there an ldap attribute for this?
      keei.setDefault(true);//is there an ldap attribute for this?
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityEmail:"
              + "(entityEmailId=" + keei.getEntityEmailId() + ")"
              + "(emailTypeCode=" + keei.getEmailTypeCode() + ")"
              + "(emailAddress=" + keei.getEmailAddress() + ")"
              + "(isActive=" + keei.isActive() + ")"
              + "(isDefault=" + keei.isDefault() + ")"
              + "(isSuppressEmail=" + keei.isSuppressEmail() + ")");
    }
    return keei;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getEmailAddressAttributeName() {
    return this.emailAddressAttributeName;
  }

  public void setEmailAddressAttributeName(String attrName) {
    this.emailAddressAttributeName = attrName;
  }

  public String getSuppressionAttributeName() {
    return this.suppressionAttributeName;
  }

  public void setSuppressionAttributeName(String attrName) {
    this.suppressionAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (emailAddressAttributeName == null) {
      String msg = "The property 'emailAddressAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (suppressionAttributeName == null) {
      String msg = "The property 'suppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
